﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlarmBoxOCX
{
    public partial class PlayerForm : UserControl
    {
        private string _ip = "114.55.227.27";
        private uint _port = 8877;
        private string _user = "runnet";
        private string _conntPwd = "runnet";
        private string _deviceId = "123456789";

        private AxP2PClientControlLib.AxP2PClientControl P2PClientControl = new AxP2PClientControlLib.AxP2PClientControl();
       // private AxWEBLib.AxWeb wxweb = new AxWEBLib.AxWeb();
        public PlayerForm()
        {
            InitializeComponent();

            //P2PClientControl.Height = this.Height;
            //P2PClientControl.Width = this.Width;
            //this.Controls.Add(P2PClientControl);

            //wxweb.Height = this.Height;
            //wxweb.Width = this.Width;
            //this.Controls.Add(wxweb);
               
        }

        public void SetDeviceId(string deviceId)
        {
            _deviceId = deviceId;
        }

        public void SetConfig(string ip, uint port, string user, string conntPwd)
        {
            _ip = ip;
            _port = port;
            _user = user;
            _conntPwd = conntPwd;
        }
        public int Connect()
        {
            var ret = P2PClientControl.Login(_ip, _port, _user, _conntPwd);
            if (ret == 0)
            {
                ret = P2PClientControl.Connect(_deviceId, _user, _conntPwd, 0, 1, 554);
            }
            return ret;
        }

        public int DisConnect()
        {
            return P2PClientControl.Disconnect();
        }
        public int StartRecordVideo()
        {
            return P2PClientControl.StartRecordVideo();
        }

        public int StopRecordVideo()
        {
            return P2PClientControl.StopRecordVideo();
        }

        public int StartTalk()
        {
            var ret = P2PClientControl.Login(_ip, _port, _user, _conntPwd);
            return P2PClientControl.StartTalk();
        }

        public int StopTalk()
        {
            return P2PClientControl.StopTalk(); 
        }

        public uint VoiceBroadcastEx()
        {
            var ret = P2PClientControl.Login(_ip, _port, _user, _conntPwd);

            /*
            //这要注意调用顺序，先调用ClearDevice清除列表 --> AddDevice添加需要广播的涉笔 --> 开启广播 VoiceBroadcast--> 	停止广播StopVoiceBroadcast--> 清空设备列表ClearDevice
            //思路是，将需要广播的设备（只需要设备ID)添加到队列中，然后开始广播，最后清空列表; 这里这样处理是便于不同的编程工具方便调用OCX接口
            //这里只能填写已经在线的设备，由外部调用者自己控制。
            */
            P2PClientControl.ClearDeviceEx();

            uint iDeviceCount = 5;
            //这里添加的设备ID应该是在线的设备ID， iDeviceCount需要添加设备的总数量，添加的中途不要修改
            P2PClientControl.AddDeviceEx("12345678", iDeviceCount);
            P2PClientControl.AddDeviceEx("201606280714245", iDeviceCount);
            P2PClientControl.AddDeviceEx("20160623060359", iDeviceCount);
            P2PClientControl.AddDeviceEx("20160623060208", iDeviceCount);
            P2PClientControl.AddDeviceEx("20160623060208", iDeviceCount);

            //开启广播
            var iRet = P2PClientControl.VoiceBroadcastEx();
            return iRet;
        }

        public uint StopVoiceBroadcastEx()
        {
            //停止广播
            P2PClientControl.StopVoiceBroadcastEx();

            //清空设备列表
            return P2PClientControl.ClearDeviceEx();
        }


        public int SubscriberAlarmEx()
        {
            var ret = P2PClientControl.Login(_ip, _port, _user, _conntPwd);

            //这里填入的参数是在线设备的ID， 这里只演示了一个设备，要订阅多个设备的报警可以循环调用
            return P2PClientControl.SubscriberAlarmEx("12345678");

            //多个设备报警订阅
            /*
            var i = 0;
            for(i=0; i<100;i++)
            {
                P2PClientControl.SubscriberAlarmEx("2016062307411xxx");
            }
            */
        }

        public int UnSubScriberAlarmEx()
        {
            //取消单个设备， 也可以通过循环取消多个设备
            return P2PClientControl.UnSubScriberAlarmEx("12345678");

            //直接取消所有订阅
            //P2PClientControl.UnSubscriberAlarmAll();
        }
    }
}
